import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../text_high_light.dart';

class AgreementComp extends StatefulWidget {
  final String name;
  List<String> searchContent;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final Color? color;
  bool isCheck = false;
  void Function(bool)? onSelect;
  void Function(int)? searchContentClick;

  AgreementComp({
    super.key,
    this.margin = const EdgeInsets.only(left: 0, right: 0),
    this.padding = const EdgeInsets.only(left: 20, right: 20, bottom: 11, top: 11),
    this.color = Colors.transparent,
    this.isCheck = false,
    this.searchContent = const [],
    this.name = '',
    this.onSelect,
    this.searchContentClick,
  });

  @override
  _AgreementCompState createState() => _AgreementCompState();
}

class _AgreementCompState extends State<AgreementComp> {
  bool isCheck = false;

  get searchContentClick => widget.searchContentClick;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      isCheck = widget.isCheck;
    });
  }

  get name => widget.name;

  get searchContent => widget.searchContent;

  void onSelect(bool value) {
    setState(() {
      isCheck = value;
    });
    if (widget.onSelect != null) {
      widget.onSelect!(value);
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: widget.margin,
      padding: widget.padding,
      color: widget.color,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
              width: 20.w,
              height: 20.w,
              margin: EdgeInsets.only(right: 10.w),
              child: Checkbox(
                  value: isCheck,
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  activeColor: Color.fromRGBO(134, 103, 254, 1),
                  side: BorderSide(color: Color.fromRGBO(245, 245, 245, 1)),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4.w), // 设置圆角半径
                  ),
                  onChanged: (value) {
                    onSelect(value!);
                  })),
          Expanded(
              child: TextHighlight(
                  name,
                  searchContent,
                  TextStyle(
                      height: (16 / 12).w,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                      color: Color.fromRGBO(134, 131, 145, 1)),
                  TextStyle(
                      height: (16 / 12).w,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                      color: Color.fromRGBO(134, 103, 254, 1)),
                  maxLines: null,
                  overflow: TextOverflow.visible, searchContentClick: (int index) {
            searchContentClick.call(index);
          }, unSearchClick: () {
            onSelect(!this.isCheck);
          })),
        ],
      ),
    );
  }
}
